package com.newway.back.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newway.back.entity.ArticleComments;
import com.newway.back.entity.Collections;
import com.newway.back.mapper.ArticleCommentsMapper;
import com.newway.back.mapper.CollectionsMapper;
import com.newway.common.context.UserInfo;
import com.newway.common.context.UserInfoContext;
import com.newway.common.result.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/system/comment")
public class ArticleCommentsController {


    @Resource
    ArticleCommentsMapper articleCommentsMapper;

    @Resource
    CollectionsMapper collectionsMapper;

    @GetMapping("/list")
    public Result list(ArticleComments articleComments){
        Page<ArticleComments> articleCommentsPage = new Page<>(articleComments.getPageNum(),articleComments.getPageSize());
        QueryWrapper<ArticleComments> articleCommentsQueryWrapper = new QueryWrapper<>();
        if(articleComments.getArticleId()!=null){
            articleCommentsQueryWrapper.eq("article_id",articleComments.getArticleId());
        }
        articleCommentsQueryWrapper.orderByDesc("create_time");
        Page<ArticleComments> result = articleCommentsMapper.selectPage(articleCommentsPage, articleCommentsQueryWrapper);
        return Result.success(result);
    }

    @PostMapping("/add")
    public Result add(@RequestBody ArticleComments articleComments){
        UserInfo userInfo = UserInfoContext.getUserInfo();
        articleComments.setCreateTime(LocalDateTime.now());
        articleComments.setUserAvatar(userInfo.getAvatar());
        articleComments.setUserName(userInfo.getUserName());
        articleComments.setUserId(userInfo.getId());
        articleCommentsMapper.insert(articleComments);
        return Result.success();
    }

    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id){
        articleCommentsMapper.deleteById(id);
        return Result.success();
    }

    @GetMapping("/isCollection/{articleId}")
    public Result zanAndCollection(@PathVariable("articleId") Integer articleId){
        QueryWrapper<Collections> collectionsQueryWrapper = new QueryWrapper<>();
        collectionsQueryWrapper.eq("article_id",articleId);
        collectionsQueryWrapper.eq("user_id",UserInfoContext.getUserInfo().getId());
        Collections collections = collectionsMapper.selectOne(collectionsQueryWrapper);
        if(collections==null){
            collections = new Collections();
            collections.setArticleId(articleId);
            collections.setUserId(UserInfoContext.getUserInfo().getId());
            collectionsMapper.insert(collections);
        }
        return Result.success(collections);
    }


    /**
     * 点赞或收藏
     * @param id
     * @return
     */
    @PostMapping("/addCollection")
    public Result addCollection(@RequestBody Collections collections){
        collectionsMapper.updateById(collections);
        return Result.success();
    }
}
